import {
	uniAdminCacheKey
} from '../constants.js'
import config from '@/admin.config.js'

export default {
	namespaced: true,
	state: {
		inited: false,
		navMenu: [],
		routes: [],
		theme: uni.getStorageSync(uniAdminCacheKey.theme) || 'default',
		appName: process.env.UNI_APP_NAME || '',
		appid: process.env.UNI_APP_ID || ''
	},
  getters:{
    currentThemeName(state){
      let text = "";
      config.navBar.themes.forEach(theme => {
        if(state.theme == theme.value){
          text = theme.text
        }
      });
      return text;
    }
  },
	mutations: {
		SET_APP_NAME: (state, appName) => {
			state.appName = appName
		},
		SET_NAV_MENU: (state, navMenu) => {
			state.inited = true
			state.navMenu = navMenu
		},
		SET_ROUTES: (state, routes) => {
			state.routes = routes
		},
    SET_LAST_ROUTE_NAME:(state, name) =>{
      if(!state.routes.length){
        console.log("未找到当前路由地址路径，如有问题请及时处理。")
        return
      }
 
      state.routes[state.routes.length - 1].name = name
    },
		SET_THEME: (state, theme) => {
			// #ifdef H5
			document
				.getElementsByTagName('body')[0]
				.setAttribute('data-theme', theme)
			// #endif
			uni.setStorageSync(uniAdminCacheKey.theme, theme)
			state.theme = theme
      console.log(state.theme)
		}
	},
	actions: {
		setAppName({
			commit
		}, appName) {
			commit('SET_APP_NAME', appName)
		},
		setRoutes({
			commit
		}, routes) {
			commit('SET_ROUTES', routes)
		}
	}
}
